#!/usr/bin/env python
# -*- coding: utf8 -*-

'''
Created on 2011-9-24

@author: zhongfeng
'''
 
import re,os,sys
import chardet
from pageparser import ObuyUrlSummary
from utils import Singleton
from ConfigParser import ConfigParser, NoOptionError

def __getUrlSumsFromSection(section):
    curPath = os.path.abspath(os.path.dirname(sys.argv[0]))
    fileName = os.path.join(curPath, 'urls.cfg')
    print 'spider.cfg full path:%s' % fileName
    urls = list()
    if not os.path.exists(fileName):
        return urls
    regx = r'\[%s\]' % section
    includeSecRegx = re.compile(regx)
    otherSecRegx = re.compile(r'\[.*\]')
    flag = False
    with file(fileName) as inputFile:
        for line in inputFile:
            encoding = chardet.detect(line)['encoding']
            line = line.decode(encoding,'ignore')
            if (not flag) and includeSecRegx.match(line):
                flag = True
            elif flag:
                if otherSecRegx.match(line):
                    break
                if line.strip() != '':
                    line = ' '.join(line.split())
                    ret = line.split(',')
                    ret = [it.strip() for it in ret]
                    urlSumm = ObuyUrlSummary(name = ret[1],url = ret[0],catagoryLevel = int(ret[2]))  
                    urls.append(urlSumm)
    return urls

def getIncludeUrlSums():
    return __getUrlSumsFromSection('include')

def getExcludeUrlSums():
    return __getUrlSumsFromSection('exclude')

class SpiderConfig(Singleton):
    @classmethod
    def _init(cls):
        curPath = os.path.abspath(os.path.dirname(sys.argv[0]))
        fileName = os.path.join(curPath, 'spider.conf')
        cls.cf = ConfigParser()  
        cls.cf.read(fileName)
    
    @classmethod
    def getConfig(cls,option):
        if not hasattr(cls, 'cf'):
            cls._init()
        try:
            return cls.cf.get('conf',option)
        except Exception:
            pass
        
    @classmethod
    def getMaxPage(cls):
        ret = cls.getConfig('max_page')
        if ret:
            return int(ret)
        return 50
    
    @classmethod
    def getThreadNum(cls):
        threadNum = cls.getConfig('thread_num')
        if threadNum:
            return int(threadNum)
        return 10
    
    @classmethod
    def getProxy(cls):
        return cls.getConfig('ftp_proxy')
    
    @classmethod
    def isStartSpider(cls):
        flag = int(cls.getConfig('is_spider'))
        if flag is not None:
            return flag
        return True
    
    @classmethod
    def isUpload(cls):
        flag = int(cls.getConfig('is_upload'))
        if flag is not None:
            return flag
        return True
        
if __name__ == '__main__':
    print SpiderConfig.getMaxPage()
    print SpiderConfig.getThreadNum()
    print SpiderConfig.getProxy()
                
            